package cn.com.nes.common.util;

import java.util.HashMap;
import java.util.Map;

import cn.com.nes.site.entity.sms.VerCodeReq;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SMSUtil {
public static void main(String[] args) {
	String phone = "15704661884";
//	Map<String,String> map = new HashMap<String, String>();
//	map.put("username", "133333333");
//	map.put("pwd", "123456");
//	sendSMS_AGENT_REG(phone,map);
	sendSMS_ALIYUN_REG(phone,"123");
}


	// 阿里云发送用户注册
	public static VerCodeReq sendSMS_ALIYUN_REG(String phone, String verCode) {
		
		//String res = "";
		VerCodeReq sms  = null;
//		verCode = createRandom(true, 6);
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = accessKey;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = accessSecret;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName("身份验证");
			request.setTemplateCode("SMS_34955334");
			request.setTemplateParam("{\"code\":\"" + verCode
					+ "\",\"product\":\"热链云\"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				sms = new VerCodeReq();
				sms.setDeviceid("");
				sms.setPhone(phone);
				sms.setSmstype("2");
				sms.setTicketid("0001");
				sms.setVercode(verCode);

			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sms;
	}
	public static int sendAliSms(String signName, String templateCode, String phone,
								 JSONObject params) {
		VerCodeReq sms  = null;
//		verCode = createRandom(true, 6);
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		int result = 1;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			/**
			 * 短信API产品名称（短信产品名固定，无需修改）
			 */
			final String product = "Dysmsapi";
			/**
			 * 短信API产品域名（接口地址固定，无需修改
			 */
			final String domain = "dysmsapi.aliyuncs.com";
			/**
			 *  你的accessKeyId,参考本文档步骤2
			 */
			final String accessKeyId = accessKey;
			/**
			 * 你的accessKeySecret，参考本文档步骤2
			 */
			final String accessKeySecret = accessSecret;
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName(signName);
			request.setTemplateCode(templateCode);
			request.setTemplateParam(params.toString());
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				result = 0;
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	// 活动用户填写信息-模板固定创今云
	public static VerCodeReq sendSMS_ALIYUN_REG_active(String phone, String verCode) {
		
		//String res = "";
		VerCodeReq sms  = null;
//			verCode = createRandom(true, 6);
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = accessKey;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = accessSecret;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName("热链产业");
			request.setTemplateCode("SMS_34955334");
			request.setTemplateParam("{\"code\":\"" + verCode
					+ "\",\"product\":\"热链云\"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				sms = new VerCodeReq();
				sms.setDeviceid("");
				sms.setPhone(phone);
				sms.setSmstype("2");
				sms.setTicketid("0001");
				sms.setVercode(verCode);

			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sms;
	}
	
	//新注册用户提醒
	public static boolean sendSMS_AGENT_REG(String phone,Map<String,String> map) {
		
		boolean result = false;
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = accessKey;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = accessSecret;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName("创今云");
			request.setTemplateCode("SMS_241840014");
			request.setTemplateParam("{\"username\":\""+ map.get("username") +"\",\"pwd\":\"123456\"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				result = true;
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


	
	public static String createRandom(boolean numberFlag, int length) {
		String retStr = "";
		String strTable = numberFlag ? "1234567890"
				: "1234567890abcdefghijkmnpqrstuvwxyz";
		int len = strTable.length();
		boolean bDone = true;
		do {
			retStr = "";
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr += strTable.charAt(intR);
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);
		return retStr;
	}
	
	// 发送项目超时提醒
	public static void sendProjectOvertimeRemind(String phone, String projectName, String time) {
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = accessKey;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = accessSecret;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName("创今云");
			request.setTemplateCode("SMS_241066419");
			request.setTemplateParam("{\"name\":\"" + projectName
					+ "\",\"time\":\""+time+"\"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//通用模板  验证码为${code}，请勿将验证码泄露给他人。
	public static VerCodeReq sendActivityCode(String phone,Map<String,String> map) {
		VerCodeReq sms  = null;
		String accessKey = Globals.SMS_ALIYUN_APPKEY;
		String accessSecret = Globals.SMS_ALIYUN_SECRET ;
		try {
			String validCode = ValidCodeUtil.generateCode(phone);
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = accessKey;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = accessSecret;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName("热链产业");
			request.setTemplateCode("SMS_461320438");
			request.setTemplateParam("{\"code\":"+validCode+"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				sms = new VerCodeReq();
				sms.setDeviceid("");
				sms.setPhone(phone);
				sms.setSmstype("2");
				sms.setTicketid("0001");
				sms.setVercode(validCode);
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sms;
	}
	public static int sendSMS(int smsType, Map<String, String> param) {
		int res = 9;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			final String product = "Dysmsapi";// 短信API产品名称（短信产品名固定，无需修改）
			final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名（接口地址固定，无需修改）
			final String accessKeyId = Globals.SMS_ALIYUN_APPKEY;// 你的accessKeyId,参考本文档步骤2
			final String accessKeySecret = Globals.SMS_ALIYUN_SECRET;// 你的accessKeySecret，参考本文档步骤2
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(param.get("phone"));
			// 必填:短信签名-可在短信控制台中找到

			request.setTemplateCode(param.get("validCode"));
			request.setSignName("清洁取暖服务中心");
			switch (smsType) {
				case 1: // 1 注册验证码 3 手机号验证 内容： 验证码${code}，您正在进行${product}身份验证，请勿告诉别人！
				case 3: // 1 注册验证码 3 手机号验证 内容： 验证码${code}，您正在进行${product}身份验证，请勿告诉别人！
					request.setTemplateParam(
							"{\"code\":\"" + param.get("verCode") + "\",\"product\":\"" + param.get("product") + "\"}");
					break;
				case 2: // 2 找回密码 //验证码${code}，验证码${code}，您正在尝试修改${product}登录密码，请妥善保管账户信息。
					request.setTemplateParam(
							"{\"code\":\"" + param.get("verCode") + "\",\"product\":\"" + param.get("product") + "\"}");
					break;
				default:
					break;
			}

			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
				res = 0;
			} else {
				sendSmsResponse.getCode();
				System.out.println(sendSmsResponse.getMessage());
				res = 10;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	public static int sendMaintainSms(String accessKey, String accessSecret, String signName, String templateCode, String phone, String name) {
		int result = 1;

		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			/**
			 * 短信API产品名称（短信产品名固定，无需修改）
			 */
			final String product = "Dysmsapi";
			/**
			 * 短信API产品域名（接口地址固定，无需修改
			 */
			final String domain = "dysmsapi.aliyuncs.com";
			/**
			 *  你的accessKeyId,参考本文档步骤2
			 */
			final String accessKeyId = accessKey;
			/**
			 * 你的accessKeySecret，参考本文档步骤2
			 */
			final String accessKeySecret = accessSecret;
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName(signName);
			request.setTemplateCode(templateCode);
			request.setTemplateParam("{\"name\":\"" + name + "\"}");
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				result = 0;
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	public static int sendAliSmsCallCenter(String accessKey, String accessSecret, String signName, String templateCode, String phone,
								 com.alibaba.fastjson.JSONObject params) {
		int result = 1;
		try {
			System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
			System.setProperty("sun.net.client.defaultReadTimeout", "10000");
			/**
			 * 短信API产品名称（短信产品名固定，无需修改）
			 */
			final String product = "Dysmsapi";
			/**
			 * 短信API产品域名（接口地址固定，无需修改
			 */
			final String domain = "dysmsapi.aliyuncs.com";
			/**
			 *  你的accessKeyId,参考本文档步骤2
			 */
			final String accessKeyId = accessKey;
			/**
			 * 你的accessKeySecret，参考本文档步骤2
			 */
			final String accessKeySecret = accessSecret;
			// 初始化ascClient,暂时不支持多region（请勿修改）
			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
			DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
					domain);
			IAcsClient acsClient = new DefaultAcsClient(profile);
			// 组装请求对象
			SendSmsRequest request = new SendSmsRequest();
			// 使用post提交
			request.setMethod(MethodType.POST);
			request.setPhoneNumbers(phone);
			// 必填:短信签名-可在短信控制台中找到
			request.setSignName(signName);
			request.setTemplateCode(templateCode);
			request.setTemplateParam(params.toString());
			SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
			if (sendSmsResponse.getCode() != null
					&& sendSmsResponse.getCode().equals("OK")) {
				result = 0;
			} else {
				System.out.println(sendSmsResponse.getCode());
				System.out.println(sendSmsResponse.getMessage());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
